package com.vainycos.lotterysearch.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/22 15:33
 */
public class DateUtil {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String CRON_EXPRESSION = "s m H dd MM ?";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    /**
     * 时间加减天数
     * @param date 要处理的时间，Null则为当前时间
     * @param days 加减的天数
     * @return
     */
    public static Date calcDaysDate(Date date, int days){
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
        return c.getTime();
    }

    /**
     * 获取昨天
     * @return
     */
    public static String getYesterday(){
        Date date = calcDaysDate(null, -1);
        return parseDateToStr(YYYY_MM_DD, date);
    }

    /**
     * 获取明天
     * @return
     */
    public static String getTomorrow(){
        Date date = calcDaysDate(null, 1);
        return parseDateToStr(YYYY_MM_DD, date);
    }

    /**
     * 转换cron表达式 2022-11-23 21:00:00 = 0 0 21 23 11 ? 2022
     * @param startTime
     * @return
     */
    public static String convertExpression(String startTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            return parseDateToStr(CRON_EXPRESSION, simpleDateFormat.parse(startTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间加减分钟
     * @param strDate 要处理的时间，Null则为当前时间
     * @param minutes 加减的分钟
     * @return Date
     */
    public static String dateAddMinutes(String strDate, int minutes) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date startDate = null;
        try {
            startDate = simpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
        Date time = c.getTime();
        return simpleDateFormat.format(time);
    }
}
